import 'package:fake_handwriting/Constant.dart';
import 'package:fake_handwriting/Util.dart';
import 'package:fake_handwriting/generated/l10n.dart';
import 'package:fake_handwriting/http/http.dart';
import 'package:flutter/material.dart';

class Register extends StatefulWidget {
  const Register({Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() => RegisterPage();
}

class RegisterPage extends State<Register> {
  final _email_controller = TextEditingController();
  final _password_controller = TextEditingController();
  final _password2_controller = TextEditingController();
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _email_controller.dispose();
    _password_controller.dispose();
    _password2_controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 30),
            child: Column(
              children: [
                TextField(
                  controller: _email_controller,
                  inputFormatters: [Constant.input_filter_email],
                  cursorColor: Colors.blue,
                  decoration: InputDecoration(
                      enabledBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      focusedBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      labelText: S.of(context).email,
                      labelStyle: const TextStyle(color: Colors.blue),
                      hintText: S.of(context).inputEmail,
                      prefixIcon: const Icon(
                        Icons.mail_outline,
                        color: Colors.blue,
                      )),
                ),
                const SizedBox(
                  height: 20,
                ),
                TextField(
                  obscureText: true,
                  controller: _password_controller,
                  cursorColor: Colors.blue,
                  decoration: InputDecoration(
                      enabledBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      focusedBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      labelText: S.of(context).password,
                      labelStyle: const TextStyle(color: Colors.blue),
                      hintText: S.of(context).inputPassword,
                      prefixIcon: const Icon(
                        Icons.password_rounded,
                        color: Colors.blue,
                      )),
                ),
                const SizedBox(
                  height: 20,
                ),
                TextField(
                  obscureText: true,
                  controller: _password2_controller,
                  cursorColor: Colors.blue,
                  decoration:  InputDecoration(
                      enabledBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      focusedBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      labelText: S.of(context).inputAgain,
                      labelStyle: const TextStyle(color: Colors.blue),
                      hintText: S.of(context).inputPasswordAgain,
                      prefixIcon: const Icon(
                        Icons.password_rounded,
                        color: Colors.blue,
                      )),
                ),
                const SizedBox(
                  height: 20,
                ),
                InkWell(
                  onTap: () async {
                    if (_email_controller.text.isNotEmpty) {
                      if (_password_controller.text.isNotEmpty) {
                        if (_password2_controller.text.isNotEmpty) {
                          if (_password_controller.text ==
                              _password2_controller.text) {
                            try {
                              var res = await Http.register(
                                  _email_controller.text,
                                  _password_controller.text);
                              Util.toast(res);
                              Navigator.pop(context);
                            } catch (e) {}
                          } else {
                            Util.toast(S.of(context).passwordInconsistent);
                          }
                        } else {
                          Util.toast(S.of(context).inputPasswordAgain);
                        }
                      } else {
                        Util.toast(S.of(context).inputPassword);
                      }
                    } else {
                      Util.toast(S.of(context).inputEmail);
                    }
                  },
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 10),
                    child: Container(
                      padding: const EdgeInsets.symmetric(vertical: 15),
                      width: double.infinity,
                      decoration: const BoxDecoration(
                          color: Colors.blue,
                          borderRadius: BorderRadius.all(Radius.circular(5))),
                      child: Center(
                        child: Text(
                          S.of(context).register,
                          style: TextStyle(color: Colors.white),
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
      appBar: AppBar(
        title: Text(S.of(context).register),
      ),
    );
  }
}
